<template>
    <div class="app-container">

        <!-- 
        
        -->


        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="订单号" prop="orderId">
                <el-input v-model="queryParams.orderId" placeholder="请输入订单号" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="收货人" prop="consignee">
                <el-input v-model="queryParams.consignee" placeholder="请输入收货人" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>


        <el-table v-loading="loading" :data="noticeList" :default-expand-all="true">
            <el-table-column label="序号" align="center" type="index" width="55">
                <template #default="{ $index }">
                    {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="订单号" prop="orderId" />
            <el-table-column label="订单详情" prop="orderDetail">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)">订单详情</el-button>
                </template>
            </el-table-column>
            <el-table-column label="总金额" prop="totalAmount" />
            <el-table-column label="下单用户" prop="userName" />
            <el-table-column label="收货人" prop="consignee" />
            <el-table-column label="手机号" prop="phone" />
            <el-table-column show-overflow-tooltip label="收货地址" prop="address" />


            <el-table-column label="物流方式" prop="">
                <template #default="scope">
                    <!-- 物流方式（1快递 2配送 3自提） -->
                    <span v-if="scope.row.transportType == 1">快递</span>
                    <span v-if="scope.row.transportType == 2">配送</span>
                    <span v-if="scope.row.transportType == 3">自提</span>
                </template>
            </el-table-column>

            <el-table-column label="创建时间" prop="createTime">

            </el-table-column>
            <el-table-column label="状态" prop="isTransporting">
                <template #default="scope">
                    <span v-if="scope.row.isTransporting">已发货</span>
                    <span v-if="!scope.row.isTransporting">未发货</span>
                </template>
            </el-table-column>
            <el-table-column label="操作人员" prop="updateUserName" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" v-if="!scope.row.isTransporting" icon="Edit"
                        @click="orderOut(scope.row)">发货</el-button>
                </template>
            </el-table-column>
        </el-table>


        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
        <!-- 添加或修改物流对话框 -->
        <el-dialog :title="title" v-model="openDetail" width="780px" append-to-body>
            <el-form ref="noticeRef" :model="orderProductList" :rules="rules" label-width="80px">
                <el-table :data="orderProductList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" align="center" />
                    <el-table-column property="name" label="名称" />
                    <el-table-column property="image" label="图片">
                        <template #default="scope">
                            <img width="50" height="50" :src="scope.row.image" alt="">
                        </template>
                    </el-table-column>
                    <el-table-column property="model" label="型号" />
                    <el-table-column property="spec" label="规格" />
                    <el-table-column property="price" label="价格" />
                    <el-table-column property="count" label="数量" />
                </el-table>
            </el-form>
            <template #footer v-if="isDetailOrSend">
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>


    </div>
</template>

<script setup name="Notice">
import {
    produceApiSendList,
    produceApiSendProductList,
    produceApiSend
} from "@/api/backProduce/backProduce";


import {
    orderDetail,
} from "@/api/backOrder/backOrder.js";

const { proxy } = getCurrentInstance();
const { transport_type } = proxy.useDict(
    "transport_type"
);
const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        currentPage: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined,
    },
    rules: {
        // noticeTitle: [
        //     { required: true, message: "物流标题不能为空", trigger: "blur" },
        // ],
        // noticeType: [
        //     { required: true, message: "物流类型不能为空", trigger: "change" },
        // ],
    },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询物流列表 */
function getList() {
    loading.value = true;
    produceApiSendList(queryParams.value).then((response) => {
        noticeList.value = response.records;
        total.value = response.total;
        loading.value = false;
    });
}

const gridData = ref([])
const selectOrderList = ref([])

const orderProductList = ref([]);
const isDetailOrSend = ref(false);


// 出库
function orderOut(row) {
    let orderId = row.orderId
    openDetail.value = true;
    title.value = "发货清单";
    produceApiSendProductList(orderId).then((response) => {
        orderProductList.value = response;
        isDetailOrSend.value = true;
    })
}
function handleSelectionChange(e) {
    selectOrderList.value = e
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["noticeRef"].validate((valid) => {
        if (valid) {
            if (selectOrderList.value.length == 0) return
            let data = selectOrderList.value.map((i) => i.orderProductId)
            produceApiSend({
                orderProductIdList: data
            }).then((response) => {
                proxy.$modal.msgSuccess("成功");
                openDetail.value = false;
                getList();
            });
        }
    });
}



/** 取消按钮 */
function cancel() {
    openDetail.value = false;
    reset();
}

/** 表单重置 */
function reset() {
    form.value = {
        "orderId": "",
        "outReason": "",
        "transportType": ""
    };
    proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.currentPage = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}




/** 删除按钮操作 */
function handleDelete(row) {
    const orderPackageIds = row.orderPackageId || ids.value;
    proxy.$modal
        .confirm('是否确认删除物流编号为"' + orderPackageIds + '"的数据项？')
        .then(function () {
            return delTransport(orderPackageIds);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
}



const openDetail = ref(false);
function handleDetail(row) {

    isDetailOrSend.value = false;
    orderDetail(row.orderId).then((response) => {
        console.log(response);
        orderProductList.value = response;
        openDetail.value = true;
        title.value = "订单详情";
    });
}

getList();
</script>